/**
 * Created by nico on 2016/3/14.
 */
var async = require('async');
var config = require('../../../config.js'); //必要
var wxjsapi = require('wx_jsapi_sign'); //微信分享必要
var fs = require('fs');
var MyronWX=require('../../../lib/MyronWx.js');

var _VIEW_PATH = '../app_module/20160314/views/';

var RewardCtrl = require('../models/rewards');
module.exports = {
    //分享页面测试代码
    index: function (req, res) {
        var url = 'http://'+config.domain+'/extra'+req.url;
        console.log(url);
        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        var openid=req.AV.user.get('openid');
        var nickname = req.AV.user.get('nickname');
        var headimgurl = req.AV.user.get('headimgurl');
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
            if(UserInfo.subscribe === 0){
                console.log('没有关注，请先关注果然100');
                res.render(_VIEW_PATH+'error',{appId:'',timestamp:'',nonceStr:'',signature:'',url:url});
            }else{
                wxjsapi.getSignature(wxconfig)(url, function(error, result) {
                    if (error) {
                        res.json({
                            'error': error
                        });
                    } else {
                        res.render(_VIEW_PATH+'index',{appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url,openid:openid,nickname:nickname,headimgurl:headimgurl});
                        //res.json(result);
                    }
                });
            }
        }).catch(function(){
            res.render(_VIEW_PATH+'error');
            return ;
        });

    },
    test:function(req, res){
        var url = 'http://'+config.domain+'/extra'+req.url;
        //console.log(req.url)
        res.render(_VIEW_PATH+'index',{appId:'',timestamp:'',nonceStr:'',signature:'',url:url,openid:'',nickname:"",headimgurl:""});
    },
    shareCallback:function(req,res,next){
        var args = {
            uid:req.AV.user.id,
            openid: req.query.openid,
            imgUrl : req.query.imgUrl,
            nickname : req.query.nickname
        };
        RewardCtrl.insertProToCartByShare(args).then(function(data){
            /*if(data.code === 0){
                res.render(_VIEW_PATH+'success');
            }else{
                res.render(_VIEW_PATH+'error');
            }*/
            res.json(data)
        }).catch(function(err){
            res.json(err)
        });
    },

    giveCouponsByNew:function(req,res,next){
        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        console.log('=====giveCouponsByNew=========');
        var openid=req.AV.user.get('openid') || "";
        console.log('=====openid=========');
        console.log(openid);
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
            if(UserInfo.subscribe === 0){
                console.log('没有关注，请先关注果然100');
                res.render(_VIEW_PATH+'error',{title:"果然100"});
            }else{
                var uid = req.AV.user.id ||"";
                RewardCtrl.giveCoupons(uid,'169','（疯狂水果摊）满30减5元券',5).then(function(data){
                    console.log(data);
                    if(data.code === 0){
                        res.render('../views/user/coupon',{title:"果然100"});
                    }else{
                        res.render(_VIEW_PATH+'success',{title:"果然100",data:data});
                    }
                }).catch(function(err){
                    console.log(err);
                    res.render(_VIEW_PATH+'success',{title:"果然100",data:err});
                });
            }
        }).catch(function(err){
            console.log(err);
            res.render(_VIEW_PATH+'error',{title:"果然100"});
            return ;
        });
    }
};